const { PNG } = require("pngjs");
const fs = require("fs");
// 5N to 75N, 60E to 180E asia2022
// 65S to 75N 180W to 180E world2022
fs.createReadStream("./data/world2022.png").pipe(new PNG({
        filterType: 4,
    }))
.on("parsed", function ()
{
    let lat = 140;
    let lng = 360;
    console.log(this.width, this.height);
    console.log(this.width / 360, this.height / 140);
    fs.mkdirSync("./data/lightpollutionmap", { recursive: true });
    for (let i = 0; i < lat; i++)
    {
        fs.mkdirSync("./data/lightpollutionmap/" + i, { recursive: true });
        console.log(lat,":",i);
        for (let j = 0; j < lng; j++)
        {
            let dst = new PNG({ width: 120, height: 120 });
            for (let y = 0; y < 120; y++)
            {
                for (let x = 0; x < 120; x++)
                {
                    let idx = (120 * y + x) << 2;
                    let sidx = (this.width * (i * 120 + y) + j * 120 + x) << 2;
                    dst.data[idx] = this.data[sidx];
                    dst.data[idx + 1] = this.data[sidx + 1];
                    dst.data[idx + 2] = this.data[sidx + 2];
                    dst.data[idx + 3] = this.data[sidx + 3] ;
                }
            }
            let buffer = PNG.sync.write(dst);
            fs.writeFileSync(`./data/lightpollutionmap/${i}/${j}.png`, buffer);
        }
    }
});